package  
{
	import net.flashpunk.Entity;
	import net.flashpunk.graphics.Spritemap;
	import net.flashpunk.FP;
	import net.flashpunk.utils.Key
	import net.flashpunk.utils.Input;
	/**
	 * © 2012 TheJavaArcade.com
	 */	
	public class Fracas extends Entity 
	{		
		public var fracasSprite:Spritemap = new Spritemap(Assets.FRACAS, 25, 16);
		
		public var x_velocity:int;
		public var y_velocity:int;
		private var cameraX:int;
		private var cameraY: int;
		
		public var leftPressed:Boolean;
		public var rightPressed:Boolean;
		public var upPressed:Boolean;
		public var downPressed:Boolean;
		public var zPressed:Boolean;
		public var jumping:Boolean;			//jumping
		public var climbing:Boolean;		//clibing a vine
		public var onGround:Boolean;		//is fracas on the ground?
		public var gliding:Boolean;			//floating wit dat umbrella
		public var canDoubleJump:Boolean;	//have you met the hornet yet?
		public var doubleJumped:Boolean;	//false if havent 2nd jumped
		public var gameOver:Boolean;		//false until fracas reaches DK with the key
		
		public var direction:String;
		
		public var regeneration:int;
		public var jump:int;
		public var strength:int;
		public var speed:int;
		public var currentStr:int;
		public var grabCount:int;
		
		public function Fracas() {
			fracasSprite.add("walk_left", [1,2,3,2], 10, true);
			fracasSprite.add("walk_right", [4, 5, 6, 5], 10, true);
			fracasSprite.add("stand", [12], 1, false);
			fracasSprite.add("fall", [14, 13], 8, true);
			fracasSprite.add("jump_left", [0], 10, true);
			fracasSprite.add("jump_right", [7], 10, true);
			fracasSprite.add("climb", [16, 17], 10, true);
			fracasSprite.add("hang", [16], 1, false);
			fracasSprite.add("flip_right", [8, 9, 10, 11], 10, true);
			fracasSprite.add("flip_left", [23, 22, 21, 20], 10, true);
			
			graphic = fracasSprite;
			layer = -2;
			leftPressed = false;
			rightPressed = false;
			zPressed = false;
			jumping = false;
			climbing = false;
			onGround = false;
			gliding = false;
			canDoubleJump = false;
			doubleJumped  = false;
			gameOver = false;
			direction = "right";
			x_velocity = 0;
			y_velocity = 0;
			grabCount = 0;
			setHitbox(19, 16, -3, 0);
			type = "fracas";
			strength = 1;
			jump = 1;
			speed = 1;
			regeneration = 1;
			currentStr = strength * 12;
		}
//FRACAS UPDATE !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!		
		override public function update():void {
			if (!gameOver) {
				GameEngine.timer--;
				if (GameEngine.timer < 0) GameEngine.timer = 0;
				checkKeys();
				moveFracas();
				vineCheck();
				gravity();
				setFracas();
				animateFracas();
				strengthRegen();
			}
		}
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
		public function checkKeys():void{
		//KEY CHECKS
			if (Input.check(Key.UP)) upPressed = true; else { upPressed = false; }
			if (Input.check(Key.LEFT)) direction = "left";
			if (Input.check(Key.LEFT)) { leftPressed = true; } else { leftPressed = false; }
			if (Input.check(Key.RIGHT)) direction = "right";
			if (Input.check(Key.RIGHT)) { rightPressed = true; } else { rightPressed = false; }
			if (Input.check(Key.DOWN)) downPressed = true; else downPressed = false;
			if (Input.pressed(Key.Z)) {
				if (jumping && canDoubleJump && !doubleJumped && currentStr > 0) {//double jumping
					doubleJumped = true;
					y_velocity = -3-jump;
					currentStr = currentStr - (jump*2);
				}
				if (!jumping) {
					if (climbing) {
						climbing = false;
						y_velocity = -5 - (jump / 2);
						currentStr = currentStr - (jump*2);
						grabCount = 8;
					}else  if (onGround){
						y_velocity = -5 - jump;
					}
					jumping = true;
				}
			}
			if (Input.check(Key.Z)) zPressed = true; else zPressed = false;
		
		}
		public function moveFracas():void {
		//LATERAL MOVEMENT
			if (leftPressed && !climbing) x_velocity--;
			if (rightPressed && !climbing) x_velocity++;
			if (x_velocity > speed) x_velocity  = speed;
			if (x_velocity < -speed) x_velocity = -speed;
			if (!leftPressed && !rightPressed && x_velocity > 0) x_velocity--;
			if (!leftPressed && !rightPressed && x_velocity < 0) x_velocity++;
			while (collide("level", x + x_velocity, y) && x_velocity!=0) {
				if (x_velocity > 0) x_velocity--;
				if(x_velocity < 0) x_velocity++;
			}
		}
		public function vineCheck():void {
		//CLIMBING
			if (collide("vines", x + x_velocity, y) && upPressed && currentStr > 0 && grabCount ==0 ) {
				onGround = false;
				if (x_velocity >= 0) {
					while (collide("vines", x + x_velocity, y)) {x_velocity--;}
					x_velocity = x_velocity + 13;
				}
				if (x_velocity < 0) {
					while (collide("vines", x + x_velocity, y)) {x_velocity++;}
					x_velocity = x_velocity - 14;
				}
				climbing = true; jumping = false; doubleJumped = false;				
			}
			if (!collide("vines", x + x_velocity, y)) climbing = false;
			if (climbing) y_velocity = 0;
			if (climbing && upPressed) y_velocity = -2;
			if (climbing && downPressed) y_velocity = speed;
			if (grabCount > 0) grabCount--;
		}
		public function gravity():void {
		//GRAVITY/JUMPING
			if (y_velocity <  0) {
				while (collide("level", x, y + y_velocity)) {
					y_velocity ++;
				}
			}
			if (!climbing) y_velocity++;
			if (y_velocity > 10) y_velocity = 10;
			onGround = false;
			if (y_velocity > 0) {
				while (collide("level", x + x_velocity, y + y_velocity) && y_velocity >0) {
					y_velocity--;
					jumping = false;
					onGround = true;
					doubleJumped = false;
				}
			}
			if (x_velocity > 0) direction = "right";
			if (x_velocity < 0) direction = "left";
			if (zPressed && !climbing && GameEngine.umbrella.state > 1 && y_velocity > 2 && currentStr > 0) {//UMBRELLA GLIDING (drunk while i wrote this part so probably  could be done better)
				GameEngine.umbrella.visible = true;
				if (direction == "right")	GameEngine.umbrella.x = x + 15 + x_velocity; else GameEngine.umbrella.x = x -4 + x_velocity;
				GameEngine.umbrella.y = y - 10;
				GameEngine.umbrella.umbrellaSprite.scale = 1;
				y_velocity = 2;
				gliding = true;
				currentStr = currentStr -2;
			}else gliding = false;
		}
		public function setFracas():void {//POSTITION FRACAS & CAMERA
			//fracas
			x = x + x_velocity;
			y = y + y_velocity;
			//camera
			if (x > cameraX +(FP.width / 2) - 140) cameraX = (x + 140) - (FP.width / 2);
			if (x < cameraX + 140) cameraX = x - 140;
			if (cameraX < 0) cameraX = 0;
			if (cameraX > 1200 -320) cameraX = 1200 - 320;
			if (y_velocity > 0 && y + y_velocity > cameraY + 190) cameraY += y_velocity;
			if (cameraY > 720 - 240) cameraY = 720 - 240;
			if (y_velocity < 0 && y + y_velocity < FP.camera.y + 100) cameraY += y_velocity;
			if (cameraY < 0 ) cameraY = 0;
			FP.camera.x = cameraX;
			FP.camera.y = cameraY;
			//move static entities with the camera
			GameEngine.strengthBar.x = cameraX + (FP.width / 4) - (strength * 12)/2;
			GameEngine.strengthBar.y = cameraY + 1;
			if (GameEngine.hive.state == 1) {
				GameEngine.hive.x = FP.camera.x + 5;
				GameEngine.hive.y = FP.camera.y + 230;
			}
			if (GameEngine.file.state == 2) {
				GameEngine.file.x = FP.camera.x + 15;
				GameEngine.file.y = FP.camera.y + 230;
			}
			if (GameEngine.umbrella.state == 2 && !gliding) {
				GameEngine.umbrella.umbrellaSprite.scale = .5;
				GameEngine.umbrella.x = FP.camera.x + 15;
				GameEngine.umbrella.y = FP.camera.y + 230;
			}
			if (GameEngine.cagekey.state == 2) {
				GameEngine.cagekey.cagekeySprite.scale = .5;
				GameEngine.cagekey.x = FP.camera.x + 25;
				GameEngine.cagekey.y = FP.camera.y + 230;
			}
			GameEngine.background.x = FP.camera.x * (600/880);
			GameEngine.background.y = FP.camera.y * (360 / 480);
		}
		public function animateFracas():void {
		//ANIMATION
			if (!jumping && !climbing && !gliding && y_velocity >= 5 && !doubleJumped) fracasSprite.play("fall");	
			if (x_velocity != 0 && !jumping && !gliding && y_velocity < 5 && !doubleJumped) fracasSprite.play("walk_"+direction);
			if (x_velocity == 0 && !jumping && !climbing && !gliding && y_velocity < 5 && !doubleJumped) fracasSprite.play("stand");
			if (jumping && !doubleJumped) fracasSprite.play("jump_" + direction);
			if (gliding) fracasSprite.play("jump_" + direction);
			if (climbing && y_velocity != 0) fracasSprite.play("climb");
			if (climbing && y_velocity == 0) fracasSprite.play("hang");					
			if (doubleJumped && !gliding) fracasSprite.play("flip_" + direction);
		}
		public function strengthRegen():void {
		//STRENGTH
			if (onGround && currentStr < strength * 12) currentStr = currentStr + regeneration;
			if (climbing) currentStr--;
			if (currentStr < 1) {
				currentStr = 0;
				climbing = false;
			}
		}

		public function adjustCamera():void {
			var camX:int = x + halfWidth - (FP.width/4);
			var camY:int = y - 170;//+ halfHeight - (FP.height / 4)-75;
			if (camX < 0) camX = 0;
	//		if (camX + FP.width > 1200) camX = 1200 - (FP.width/2);
			if (camY < 0) camY = 0;
		//	if (camY + FP.height > 720) camY = 720 - (FP.height / 2);
			FP.camera.x = camX; cameraX = camX;
			FP.camera.y = camY; cameraY = camY;
		}
		
	}

}